//
//  PhotoScrollView.m
//  SDNestedTablesExample
//
//  Created by Bindx on 3/26/14.
//
//

#import "PhotoScrollView.h"
#import "NXButton.h"
@implementation PhotoScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _buttonArray = [[NSMutableArray alloc] initWithCapacity:0];
        UIScrollView * scrollview = [[UIScrollView alloc]init];
        scrollview.delegate = self;
        self.backgroundColor = [UIColor grayColor];
        self.scrollEnabled = YES;
        [self addSubview:scrollview];
        numberOfAll = 0;
        
//        UIImageView * image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_58.png"]];
//        image.frame = CGRectMake(8, 8, 33, 33);
//        [self addSubview:image];
    }
    return self;
}

-(void)addOneWithIndex:(NSIndexPath *)_indexPath withuface:(NSString *)_uface
{
    [self removeOneWithIndex:_indexPath];
    NXButton * pBtn = [NXButton buttonWithType:UIButtonTypeCustom];
//    [pBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"a%d.jpg",rand()%32+1]] forState:UIControlStateNormal];
    pBtn.frame = CGRectMake(numberOfAll*41+8, 8, 33, 33);
    pBtn.tag = _indexPath.row+1;
    pBtn.uface = _uface;
    
    [pBtn setIndexPath:_indexPath];
    pBtn.index = numberOfAll;
//    [pBtn setTitle:[NSString stringWithFormat:@"%d",pBtn.index] forState:UIControlStateNormal];
//    [pBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_buttonArray addObject:pBtn];
    [pBtn addTarget:self action:@selector(removeOne:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:pBtn];
    numberOfAll ++;
}

-(void)removeOne:(NXButton *)sender
{
//    [sender removeFromSuperview];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"didselectcell" object:sender];
//    [_buttonArray removeObject:sender];
//    [self scrollViewAutoLayout:sender.index];
    
}
-(NXButton *)searchButtonWithIndexPath:(NSIndexPath *)_indexPath
{
    
    for (NXButton *btn in _buttonArray)
    {
        if(btn.section == _indexPath.section&&btn.row == _indexPath.row)
        {
            return btn;
        }
    }
    return nil;
}
-(void)removeOneWithIndex:(NSIndexPath *)_indexPath
{
    NXButton *btn = [self searchButtonWithIndexPath:_indexPath];
//      NSLog(@"_index : %d %d  %d",_indexPath.row,_indexPath.section,btn.index);
    if(btn)
    {
        NSLog(@"_index : %d",btn.index);
        [btn removeFromSuperview];
        [_buttonArray removeObject:btn];
        [self scrollViewAutoLayout:btn.index];
    }
}

-(void)scrollViewAutoLayout:(NSInteger)_index
{
    for(int i = _index+1;i<numberOfAll+1;i++)
    {
        
        NXButton *btn = (NXButton *)[self viewWithTag:i+1];
        if(btn)
        {
            if(btn.index == 0)
            {
                
            }
            btn.frame = CGRectMake(btn.frame.origin.x-41, 8, 33, 33);
            btn.index --;
//            [btn setTitle:[NSString stringWithFormat:@"%d",btn.index] forState:UIControlStateNormal];
        }
    }
    NSLog(@"numberOfAll : %d",numberOfAll);
    numberOfAll --;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
@end
